package com.scsj.supermarket.view.activity.personalsettingmodel;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.scsj.supermarket.R;
import com.scsj.supermarket.base.UpdateUserNickNameBase;
import com.scsj.supermarket.bean.BaseBean;
import com.scsj.supermarket.presenter.UpdateUserNickNamePresenter;
import com.scsj.supermarket.view.activity.baseactivitymodel.BaseActivity;

import es.dmoral.toasty.Toasty;
import okhttp3.MediaType;
import okhttp3.RequestBody;


/**
 * 昵称修改
 */
public class ChangeNickNameActivity extends BaseActivity implements View.OnClickListener, UpdateUserNickNameBase.IView {
    private EditText nickname_et;
    private TextView confirm_tv;
    private UpdateUserNickNamePresenter updateUserNickNamePresenter;
    @Override
    protected void createView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_change_nick_name);
        initToolbar();
        setToolbarTitle("修改昵称");
        hasBack(true);
        updateUserNickNamePresenter = new UpdateUserNickNamePresenter(this);
    }
    @Override
    protected void initView() {
        nickname_et=findViewById(R.id.nickname_et);
        confirm_tv=findViewById(R.id.toolbar_title_right);
        confirm_tv.setVisibility(View.VISIBLE);
        confirm_tv.setText("保存");
    }

    @Override
    protected void initListener() {
        confirm_tv.setOnClickListener(this);
    }

    @Override
    protected void initData() {
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.toolbar_title_right:
                JSONObject obj  = new JSONObject();
                obj.put("nickname",nickname_et.getText().toString().trim());
                RequestBody body = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), obj.toString());
                updateUserNickNamePresenter.deliverData(body);
                break;
        }
    }
    @Override
    public void showSuccessMsg(String s, BaseBean baseBean) {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        bundle.putString("nickname", nickname_et.getText().toString());//添加要返回给页面1的数据
        intent.putExtras(bundle);
        setResult(Activity.RESULT_OK, intent);//返回页面1
        finish();
    }
    @Override
    public void showErrorMsg(String s) {
        Toasty.error(this,s).show();
    }
}
